<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button id="vehicleTypeBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <!--<button id="vehicleTypeBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="vehicleTypeTable" lay-filter="vehicleTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="vehicleTypeTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="vehicleTypeForm">
    <form lay-filter="vehicleTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>编码</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入托盘类型编码" type="text" maxlength="200" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入托盘类型名称" type="text" maxlength="200" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>计量单位</label>
            <div class="layui-input-block">
                <select id="unitId" name="unitId" lay-verify="required" required lay-search></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea"></textarea>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>-->
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="vehicleTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#vehicleTypeTable',
            url: config.base + 'enterprisecab/warehouse/admin/vehicleType/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 80}
                ,{field: 'code', align:'center', title: '编码'}
                ,{field: 'name', align:'center', title: '名称'}
                ,{field: 'unitName', align:'center', title: '计量单位'}
                ,{field: 'isDeleted', align:'center', title: '状态', templet: '<div>{{d.isDeleted == 0 ? "停用" : "启用"}}</div>'}
                ,{field: 'remark', align:'center', title: '备注'}
                ,{field: 'createTime', align:'center', title: '创建时间'}
                ,{align: 'center', toolbar: '#vehicleTypeTableBar', title: '操作', width: 200}
            ]]
        });

        // 工具条点击事件
        table.on('tool(vehicleTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(vehicleTypeTable)', function(obj){
            table.reload('vehicleTypeTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#vehicleTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '460px',
                offset: ['150px','730px'],
                title: data ? '修改载具类型' : '添加载具类型',
                content: $('#vehicleTypeForm').html(),
                success: function () {
                    //计量单位
                    admin.initDataSelect("enterprise/product/admin/productUnit/list", {limit: 9999}, "unitId", data ? data.unitId : null, "计量单位", null, null);
                    form.val('vehicleTypeForm', data);

                    // 表单提交事件
                    form.on('submit(vehicleTypeFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted = $("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        admin.req(data ? 'enterprisecab/warehouse/admin/vehicleType/update' : 'enterprisecab/warehouse/admin/vehicleType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('vehicleTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: ['20%','45%'],
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/warehouse/admin/vehicleType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

    });
</script>